library(plyr)
library(igraph)
library(fmsb)
library(Hmisc)
library(MASS)
library(stargazer)
library(tidyverse)
library(multcomp)

library(gsynth)
data(gsynth)
library(panelView)

setwd("~/Dropbox/China Huawei Paper/Production Materials/Replication/")
figdir = "~/Dropbox/China Huawei Paper/Production Materials/Figures/"

master.data <- read.csv("dataFile_China_Huawei.csv"); head(master.data)

library(fect)
data(fect)


# 1 million
dat <- master.data
dat$treated = 0
threshold=1000000
dat = as.data.frame(dat %>%
  group_by(cabb) %>%
  dplyr::mutate(treated = ifelse(row_number() == 1, 0, NA),
         treated = ifelse(china.huawei > threshold, 1, treated)) %>%
  fill(treated))
dat$treated[is.na(dat$treated)==T] <- 0

sub = dat[dat$v2x_polyarchy<=0.42,]
sub = sub[sub$cabb!="MNG",]
sub = sub[sub$cabb!="SSD",]
sub = sub[is.na(sub$v2smgovfilprc)==F & is.na(sub$treated)==F & is.na(sub$v2x_polyarchy)==F & is.na(sub$pres.election)==F & is.na(sub$coup.attempts)==F & is.na(sub$successful.coups)==F,]
sub = sub[is.na(sub$pcGDPcur)==F & is.na(sub$GDPcur)==F & is.na(sub$electricitypc)==F,]
nrow(dat);nrow(sub)


### Internet Filtering
out.mc <- fect(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, 
               data = sub, index = c("cabb","year"), 
               force = "two-way", method = "mc", CV = TRUE, 
               se = TRUE, nboots = 200, parallel = TRUE)
plot(out.mc, main = "Estimated ATT (MC)")

out.mc.p <- fect(v2smgovfilprc ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, 
                 data = sub, index = c("cabb","year"),
                 force = "two-way", method = "mc",  lambda = out.mc$lambda.cv, 
                 CV = 0, parallel = TRUE, se = TRUE,
                 nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0))
plot(out.mc.p, vis = "none", cex.text = 0.8, stats = c("placebo.p","equiv.p"),xlab="Years Since Treatment Began",ylab="Estimated ATT",main = "Internet Filtering")
dev.print(file=paste(figdir,"gsynthPlacebov2smgovfilprc.pdf",sep=""), device=pdf); dev.off()

#### Internet Shutdowns
out.mc <- fect(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, 
               data = sub, index = c("cabb","year"), 
               force = "two-way", method = "mc", CV = TRUE, 
               se = TRUE, nboots = 200, parallel = TRUE)
plot(out.mc, main = "Estimated ATT (MC)")

out.mc.p <- fect(v2smgovshut ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, 
                 data = sub, index = c("cabb","year"),
                 force = "two-way", method = "mc",  lambda = out.mc$lambda.cv, 
                 CV = 0, parallel = TRUE, se = TRUE,
                 nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0))
plot(out.mc.p, vis = "none", cex.text = 0.8, stats = c("placebo.p","equiv.p"),xlab="Years Since Treatment Began",ylab="Estimated ATT",main = "Internet Shutdowns")
dev.print(file=paste(figdir,"gsynthPlacebov2smgovshut.pdf",sep=""), device=pdf); dev.off()

##### Social Media Monitoring
out.mc <- fect(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, 
               data = sub, index = c("cabb","year"), 
               force = "two-way", method = "mc", CV = TRUE, 
               se = TRUE, nboots = 200, parallel = TRUE)
plot(out.mc, main = "Estimated ATT (MC)")

out.mc.p <- fect(v2smgovsmmon ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, 
                 data = sub, index = c("cabb","year"),
                 force = "two-way", method = "mc",  lambda = out.mc$lambda.cv, 
                 CV = 0, parallel = TRUE, se = TRUE,
                 nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0))
plot(out.mc.p, vis = "none", cex.text = 0.8, stats = c("placebo.p","equiv.p"),xlab="Years Since Treatment Began",ylab="Estimated ATT",main = "Social Media Monitoring")
dev.print(file=paste(figdir,"gsynthPlacebov2smgovsmmon.pdf",sep=""), device=pdf); dev.off()

#### Arrests for Content
out.mc <- fect(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, 
               data = sub, index = c("cabb","year"), 
               force = "two-way", method = "mc", CV = TRUE, 
               se = TRUE, nboots = 200, parallel = TRUE)
plot(out.mc, main = "Estimated ATT (MC)")

out.mc.p <- fect(v2smarrest ~ treated + v2x_polyarchy + pres.election + coup.attempts + successful.coups + icews.protest + icews.repression + GDPcur + pcGDPcur + electricitypc, 
                 data = sub, index = c("cabb","year"),
                 force = "two-way", method = "mc",  lambda = out.mc$lambda.cv, 
                 CV = 0, parallel = TRUE, se = TRUE,
                 nboots = 200, placeboTest = TRUE, placebo.period = c(-2, 0))
plot(out.mc.p, vis = "none", cex.text = 0.8, stats = c("placebo.p","equiv.p"),xlab="Years Since Treatment Began",ylab="Estimated ATT",main = "Arrests for Political Content")
dev.print(file=paste(figdir,"gsynthPlacebov2smarrest.pdf",sep=""), device=pdf); dev.off()

